//
//  ViewController.m
//  navdemo
//
//  Created by douweichao on 15/7/7.
//  Copyright (c) 2015年 douweichao. All rights reserved.
//

#import "ViewController.h"
#import "NextViewController.h"

@interface ViewController ()

@end

@implementation ViewController


- (id)init{
    if (self = [super init]) {
        
        self.title = @"第一个页面";
        self.view.backgroundColor = [UIColor blueColor];
        
        self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
        
        
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, 80, 60);
        btn.backgroundColor = [UIColor redColor];
        btn.center = CGPointMake(self.view.center.x, self.view.center.y-60);
        [btn setTitle:@"下一步" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
        
        btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, 80, 60);
        btn.backgroundColor = [UIColor redColor];
        btn.center = CGPointMake(self.view.center.x, self.view.center.y+60);
        [btn setTitle:@"右移" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(move) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
        self.moveBtn = btn;
        
    }
    
    return self;
    
}


//跳转到下一个页面
- (void)next{
    [self.navigationController pushViewController:[[NextViewController alloc] init] animated:YES];
}


- (void)move{
    
    [UIView animateWithDuration:0.35 animations:^{
        CGRect rect = self.navigationController.view.frame;
        if (rect.origin.x == 0) {
            [self.moveBtn setTitle:@"左移" forState:UIControlStateNormal];
            rect.origin.x = 150;
            rect.origin.y = 30;
            self.navigationController.view.frame = rect;
        }else{
            [self.moveBtn setTitle:@"右移" forState:UIControlStateNormal];
            CGRect rect = self.navigationController.view.frame;
            rect.origin.x = 0;
            rect.origin.y = 0;
            self.navigationController.view.frame = rect;
        }
    }];
    
    
}

- (void)hasBeenMoved{
    CGRect rect = self.navigationController.view.frame;
    if (rect.origin.x == 0) {
        [self.moveBtn setTitle:@"右移" forState:UIControlStateNormal];
    }else{
        [self.moveBtn setTitle:@"左移" forState:UIControlStateNormal];
    }
}



- (void)viewDidAppear:(BOOL)animated{
    //页面显示完毕，直接隐藏导航栏
    self.navigationController.navigationBarHidden = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hasBeenMoved) name:@"MainViewHasBeenMoved" object:nil];
}



- (void)viewWillDisappear:(BOOL)animated{
    //页面消失前，显示导航栏
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"MainViewHasBeenMoved" object:nil];
}

@end
